<?php


function getCityFromCurrentCountry(){
 	 	
	if(isset($_POST['select-choice-0']) ){
	
		// hier Funktion der DB aufrufen 
		getCities($_POST['select-choice-0']);
	}else{
		//TODO Ausgabe ans Frontend
		echo "Land nicht gesetzt\n "; 				
	}	 
}

/**
 * Get all available countries that are inserted into DB
 */
function getCities($country){
	
	// Datenbankverbindung aufbauen
	$db = new mysqli('localhost', 'root', '', 'cityguide');
	$db->set_charset("utf8");
	if (mysqli_connect_errno()) {
    	printf("Connect failed: %s\n", mysqli_connect_error());
    	exit();
	}
	
	$query = "SELECT distinct City.name FROM City, COUNTRY where COUNTRY.name = ? and CITY.ci_id = COUNTRY.ci_id";

	if ($stmt = $db->prepare($query)) {
		$stmt->bind_param('s', $country); # sind die Parameter die mit ? vordefiniert sind
		$stmt->execute();
    	$stmt->bind_result($city);
    	
    	echo "<option value='empty'> Bitte wählen.... </option>";
    	while ($row = $stmt->fetch()) {
    		
    		if(isset($_POST['select-choice-1']) ){
    			$selCity= $_POST['select-choice-1'];
	    		if ($selCity == $city) { 
	              	$selected = 'selected="selected"';
	    			$_SESSION['isCity'] = true;    
	    			$_SESSION['selectedCity'] = $city;		
	              	echo "<option value='$city' ". $selected ." >" . $city. "</option>";
	          	}
	          	else{
    				echo "<option value='". $city ."'>" . $city . "</option>";
          		}    		
    		}  else{
    			echo "<option value='". $city ."'>" . $city . "</option>";
          	}    		
    	
    	
    	}	
    	// Statement schliessen
		$stmt->close();
	}
	// Datenbankverbindung schliesssen
	$db->close();	
}

?>